<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>Executes a query against a given database and returns a result handle</title>

 </head>
 <body><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="function.sqlite-prev.html">sqlite_prev</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="function.sqlite-rewind.html">sqlite_rewind</a></div>
 <div class="up"><a href="ref.sqlite.html">SQLite 函数</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div><hr /><div id="function.sqlite-query" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">sqlite_query</h1>
  <h1 class="refname">SQLiteDatabase::query</h1>
  <p class="verinfo">(PHP 5 &lt; 5.4.0, PECL sqlite &gt;= 1.0.0)</p><p class="refpurpose"><span class="refname">sqlite_query</span> -- <span class="refname">SQLiteDatabase::query</span> &mdash; <span class="dc-title">Executes a query against a given database and returns a result handle</span></p>

 </div>

 <div class="refsect1 description" id="refsect1-function.sqlite-query-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="type">resource</span> <span class="methodname"><strong>sqlite_query</strong></span>
    ( <span class="methodparam"><span class="type">resource</span> <code class="parameter">$dbhandle</code></span>
   , <span class="methodparam"><span class="type">string</span> <code class="parameter">$query</code></span>
   [, <span class="methodparam"><span class="type">int</span> <code class="parameter">$result_type</code><span class="initializer"> = SQLITE_BOTH</span></span>
   [, <span class="methodparam"><span class="type">string</span> <code class="parameter reference">&$error_msg</code></span>
  ]] )</div>

  <div class="methodsynopsis dc-description">
   <span class="type">resource</span> <span class="methodname"><strong>sqlite_query</strong></span>
    ( <span class="methodparam"><span class="type">string</span> <code class="parameter">$query</code></span>
   , <span class="methodparam"><span class="type">resource</span> <code class="parameter">$dbhandle</code></span>
   [, <span class="methodparam"><span class="type">int</span> <code class="parameter">$result_type</code><span class="initializer"> = SQLITE_BOTH</span></span>
   [, <span class="methodparam"><span class="type">string</span> <code class="parameter reference">&$error_msg</code></span>
  ]] )</div>

  <p class="para rdfs-comment">面向对象风格 (method):</p>
  <div class="methodsynopsis dc-description">
   <span class="modifier">public</span> <span class="type"><span class="type SQLiteResult">SQLiteResult</span></span> <span class="methodname"><strong>SQLiteDatabase::query</strong></span>
    ( <span class="methodparam"><span class="type">string</span> <code class="parameter">$query</code></span>
   [, <span class="methodparam"><span class="type">int</span> <code class="parameter">$result_type</code><span class="initializer"> = SQLITE_BOTH</span></span>
   [, <span class="methodparam"><span class="type">string</span> <code class="parameter reference">&$error_msg</code></span>
  ]] )</div>

  <p class="para rdfs-comment">
   Executes an SQL statement given by the <em><code class="parameter">query</code></em> against
   a given database handle.
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.sqlite-query-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>

    
     <dt>
<em><code class="parameter">dbhandle</code></em></dt>

     <dd>

      <p class="para">
       The SQLite Database resource; returned from
       <span class="function"><a href="function.sqlite-open.html" class="function">sqlite_open()</a></span> when used procedurally. This parameter
       is not required when using the object-oriented method.
      </p>
     </dd>

    
    
     <dt>
<em><code class="parameter">query</code></em></dt>

     <dd>

      <p class="para">
       The query to be executed.
      </p>
      <p class="para">
       Data inside the query should be <a href="function.sqlite-escape-string.html" class="link">properly escaped</a>.
      </p>
     </dd>

    
    
     <dt>
<em><code class="parameter">result_type</code></em></dt>

     <dd>

      <p class="para">可选的 <em><code class="parameter">result_type</code></em>
参数接受常量，且决定返回的数组如何被索引。使用
<strong><code>SQLITE_ASSOC</code></strong> 会仅返回关联索引（已命名字段），而
<strong><code>SQLITE_NUM</code></strong> 会仅返回数值索引。<strong><code>SQLITE_BOTH</code></strong>
会同时返回关联和数值索引。<strong><code>SQLITE_BOTH</code></strong> 是此函数的默认值。</p>
     </dd>

    
    
     <dt>
<em><code class="parameter">error_msg</code></em></dt>

     <dd>

      <p class="para">
       The specified variable will be filled if an error occurs. This is
       specially important because SQL syntax errors can&#039;t be fetched using
       the <span class="function"><a href="function.sqlite-last-error.html" class="function">sqlite_last_error()</a></span> function.
      </p>
     </dd>

    

   </dl>

  </p>
  <blockquote class="note"><p><strong class="note">Note</strong>: <span class="simpara">为兼容其他数据库扩展(比如 MySQL)，支持两种可替代的语法。推荐第一种格式，函数的第一个参数是<em><code class="parameter">dbhandle</code></em>。</span></p></blockquote>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.sqlite-query-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   This function will return a result handle 或者在失败时返回 <strong><code>FALSE</code></strong>.
   For queries that return rows, the result handle can then be used with
   functions such as <span class="function"><a href="function.sqlite-fetch-array.html" class="function">sqlite_fetch_array()</a></span> and
   <span class="function"><a href="function.sqlite-seek.html" class="function">sqlite_seek()</a></span>.
  </p>
  <p class="para">
   Regardless of the query type, this function will return <strong><code>FALSE</code></strong> if the
   query failed.
  </p>
  <p class="para">
   <span class="function"><strong>sqlite_query()</strong></span> returns a buffered, seekable result
   handle.  This is useful for reasonably small queries where you need to
   be able to randomly access the rows.  Buffered result handles will
   allocate memory to hold the entire result and will not return until it
   has been fetched.  If you only need sequential access to the data, it is
   recommended that you use the much higher performance
   <span class="function"><a href="function.sqlite-unbuffered-query.html" class="function">sqlite_unbuffered_query()</a></span> instead.
  </p>
 </div>


<div class="refsect1 changelog" id="refsect1-function.sqlite-query-changelog">
  <h3 class="title">更新日志</h3>
  <p class="para">
   <table class="doctable informaltable">
    
     <thead>
      <tr>
       <th>版本</th>
       <th>说明</th>
      </tr>

     </thead>

     <tbody class="tbody">
      <tr>
       <td>5.1.0</td>
       <td>
        Added the <em><code class="parameter">error_msg</code></em> parameter
       </td>
      </tr>

     </tbody>
    
   </table>

  </p>
 </div>


 <div class="refsect1 notes" id="refsect1-function.sqlite-query-notes">
  <h3 class="title">注释</h3>
  <div class="warning"><strong class="warning">Warning</strong>
   <p class="simpara">
    SQLite <em class="emphasis">will</em> execute multiple queries separated by
    semicolons, so you can use it to execute a batch of SQL that you have
    loaded from a file or have embedded in a script. However, this works only
    when the result of the function is not used - if it is used, 
    only the first SQL statement would be executed. Function
    <span class="function"><a href="function.sqlite-exec.html" class="function">sqlite_exec()</a></span> will always execute multiple SQL 
    statements.
   </p>
   <p class="simpara">
    When executing multiple queries, the return value of this function
    will be <strong><code>FALSE</code></strong> if there was an error, but undefined otherwise (it might
    be <strong><code>TRUE</code></strong> for success or it might return a result handle).
   </p>
  </div>
 </div>


 <div class="refsect1 seealso" id="refsect1-function.sqlite-query-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="function"><a href="function.sqlite-unbuffered-query.html" class="function" rel="rdfs-seeAlso">sqlite_unbuffered_query()</a> - Execute a query that does not prefetch and buffer all data</span></li>
    <li class="member"><span class="function"><a href="function.sqlite-array-query.html" class="function" rel="rdfs-seeAlso">sqlite_array_query()</a> - Execute a query against a given database and returns an array</span></li>
   </ul>
  </p>
 </div>

</div><hr /><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="function.sqlite-prev.html">sqlite_prev</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="function.sqlite-rewind.html">sqlite_rewind</a></div>
 <div class="up"><a href="ref.sqlite.html">SQLite 函数</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div></body></html>
